package com.signway.module_signway_adapter_Api;

import android.content.Context;
//import android.os.signwaymanager.SignwayManager;
import android.util.Log;

import com.signway.open.api.IOpenAuthorizedApi;
import com.signway.opensdk.internal.utils.SDKHardwareResult;
import com.signway.opensdk.model.SDKResult;
import com.signway.opensdk.sdk.SDK;
//import com.signway.opensdk.internal.utils.SDKHardwareResult;
//import com.signway.opensdk.model.SDKResult;
//import com.signway.opensdk.sdk.SDK;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2011-2019, by your Signway, All rights reserved.
 * -----------------------------------------------------------------
 *
 * ProjectName: droid5119
 *
 * Author: Administrator
 *
 * Email: pf.wang@Signway.cn
 *
 * Description:
 *
 * -----------------------------------------------------------------
 * 2019-8-31 : Create T960xAuthorizedApi.java
 * -----------------------------------------------------------------
 */
public class SignwayAuthorizedApi implements IOpenAuthorizedApi {
    private static final String TAG = "SignwayAuthorizedApi";
    private static SignwayAuthorizedApi mSignwayAuthorizedApi;
    private static Context mContext;

    private final int dm2016AuthPos = 10;          //激活类型占了3位，起始位置：第10位
    private final int dm2016AuthDatePos = 13;      //累计播放时长占了8位，起始位置：第13位
    private final int dm2016AuthEndTimePos = 21;   //授权总时长占了8位，起始位置：第21位
    private final int dm2016AuthEndDatePos = 29;   //授权结束日期占了10位，起始位置：第29位
    private final int dm2016AuthStartDatePos = 39;   //授权开始日期占了10位，起始位置：第39位


    public static synchronized SignwayAuthorizedApi getInstance(Context context) {
        mContext = context;
        if (mSignwayAuthorizedApi == null) {
            mSignwayAuthorizedApi = new SignwayAuthorizedApi(context);
        }
        return mSignwayAuthorizedApi;
    }

    public SignwayAuthorizedApi(Context context) {
        this.mContext = context;
    }

    @Override
    public int SystemAuthorizationWay() {
//        int way = Econfig.Econfig_System_Auth_Way;
//        return way;
        return 1;  //EEPROM     ///文件授权0     1
    }


    /**
     * 设置激活状态
     *
     * @param value
     */
    @Override
    public void setAuthType(int value) {
        byte[] buff = new byte[32];
        int length = 0;
        for (int i = 0; i < 32; i++) {
            buff[i] = (byte) 0xff;
        }
        String strAuth = value + "";
        int strLen = strAuth.length();
        if (strLen != 3) {
            for (int i = 0; i < 3 - strLen; i++) {
                strAuth = "0" + strAuth;
            }
        }
        byte[] buffer = strAuth.getBytes();
        int len = buffer.length;
        if (buffer.length > 3) {
            len = 3;
        }
        try {
            System.arraycopy(buffer, 0, buff, 0, buffer.length);
        } catch (Exception e) {
            for (int i = 0; (i < buffer.length) && (i < len); i++) {
                buff[i] = buffer[i];
            }
            Log.w(TAG, "set auth error: " + e.toString());
        }
        byte[] temp = new byte[2];
        for (int i = 0; i < len; i++) {
            int total = 1;
            temp[0] = buff[i];
            SDKResult<Integer> integerSDKResult = SDK.hardwareAPI().eepromWrite(dm2016AuthPos + i, temp, total);
            length = integerSDKResult.getContent();
            if (length != total) {
                Log.e(TAG, "write setAuth failed");
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }


    /**
     * 获取激活状态
     *
     * @param
     */
    @Override
    public int getAuthType() {
        byte[] buff = new byte[32];
        int len = 0;
        int length = 0;
        len = 3;
        SDKHardwareResult mSDKHardwareResult = SDK.hardwareAPI().eepromRead(dm2016AuthPos, len);
        buff = mSDKHardwareResult.getByteArray();
        length = mSDKHardwareResult.getByteArray().length;
        if (length != len) {
            Log.e(TAG, "read getAuth failed");
        }
        int auth = 0;
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buff[i] == -1) {
                    break;
                }
            }
            auth = Integer.parseInt(new String(buff, 0, off + 1));
        } catch (Exception e) {
            Log.i(TAG, "get auth failed: " + e.toString());
            auth = 0;
        }

        if ((auth != 0x00) && (auth != 0xA0) && (auth != 0xAB)) {
            auth = 0x00;
        }
        Log.e(TAG, "auth =" + auth);
        return auth;
    }


    /**
     * 设置授权总时长
     *
     * @param
     */
    @Override
    public void setAuthTotalTime(byte[] buffer, int buf_len) {
        byte[] buff = new byte[32];
        int length = 0;

        for (int i = 0; i < 32; i++) {
            buff[i] = (byte) 0xff;
        }

        int len = buf_len;
        if (buf_len > 8) {
            len = 8;
        }
        try {
            System.arraycopy(buffer, 0, buff, 0, buf_len);
        } catch (Exception e) {
            for (int i = 0; (i < buf_len) && (i < len); i++) {
                buff[i] = buffer[i];
            }
            Log.w(TAG, "set auth time error: " + e.toString());
        }
        byte[] temp = new byte[2];
        for (int i = 0; i < len; i++) {
            int total = 1;
            temp[0] = buff[i];
            SDKResult<Integer> integerSDKResult = SDK.hardwareAPI().eepromWrite(dm2016AuthEndTimePos + i, temp, total);
            length = integerSDKResult.getContent();
            if (length != total) {
                Log.e(TAG, "write setAuthEndTime failed");
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }


    /**
     * 设置授权总时长
     *
     * @param
     */
    @Override
    public void setAuthTotalTime(long value) {
        String strTime = value + "";
        int len = strTime.length();
        if (len != 8) {
            for (int i = 0; i < 8 - len; i++) {
                strTime = "0" + strTime;
            }
        }
        byte[] buffer = strTime.getBytes();
        setAuthTotalTime(buffer, buffer.length);
    }


    /**
     * 获取授权总时长
     *
     * @param
     */
    @Override
    public long getAuthTotalTime() {
        byte[] buff = new byte[32];
        int len = 0;
        int length = 0;

        len = 8;
        SDKHardwareResult mSDKHardwareResult = SDK.hardwareAPI().eepromRead(dm2016AuthEndTimePos, len);
        buff = mSDKHardwareResult.getByteArray();
        length = mSDKHardwareResult.getByteArray().length;
        if (length != len) {
            Log.e(TAG, "read getAuthEndTime failed");
        }

        long time = 0;
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buff[i] == -1) {
                    break;
                }
            }
            time = Long.parseLong(new String(buff, 0, off + 1));
        } catch (Exception e) {
            Log.i(TAG, "get total time failed: " + e.toString());
            time = 0;
        }
        Log.e(TAG, "total time =" + time);
        return time;
    }


    /**
     * 设置授权失效日期
     *
     * @param
     */
    @Override
    public void setAuthEndDate(byte[] buffer, int buf_len) {
        byte[] buff = new byte[32];
        int length = 0;

        for (int i = 0; i < 32; i++) {
            buff[i] = (byte) 0xff;
        }

        int len = buf_len;
        if (buf_len > 10) {
            len = 10;
        }
        try {
            System.arraycopy(buffer, 0, buff, 0, buf_len);
        } catch (Exception e) {
            for (int i = 0; (i < buf_len) && (i < len); i++) {
                buff[i] = buffer[i];
            }
            Log.w(TAG, "set auth end date error: " + e.toString());
        }
        byte[] temp = new byte[2];
        for (int i = 0; i < len; i++) {
            int total = 1;
            temp[0] = buff[i];
            SDKResult<Integer> integerSDKResult = SDK.hardwareAPI().eepromWrite(dm2016AuthEndDatePos + i, temp, total);
            length = integerSDKResult.getContent();
            if (length != total) {
                Log.e(TAG, "write setAuthEndDate failed");
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

    @Override
    public void setAuthEndDate(String date) {
        byte[] buffer = date.getBytes();
        setAuthEndDate(buffer, buffer.length);
    }

    //获取授权失效日期
    @Override
    public String getAuthEndDate() {
        byte[] buff = new byte[32];
        int len = 0;
        int length = 0;

        len = 10;
        SDKHardwareResult mSDKHardwareResult = SDK.hardwareAPI().eepromRead(dm2016AuthEndDatePos, len);
        buff = mSDKHardwareResult.getByteArray();
        length = mSDKHardwareResult.getByteArray().length;
        if (length != len) {
            Log.e(TAG, "read getAuthEndDate failed");
        }

        String endDate = null;
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buff[i] == -1) {
                    break;
                }
            }
            endDate = new String(buff, 0, off + 1);
        } catch (Exception e) {
            Log.i(TAG, "get auth end date failed: " + e.toString());
            endDate = null;
        }
        Log.e(TAG, "endDate =" + endDate);
        return endDate;
    }

    // 设置授权开始日期
    @Override
    public void setAuthStartDate(byte[] buffer, int buf_len) {
        byte[] buff = new byte[32];
        int length = 0;

        for (int i = 0; i < 32; i++) {
            buff[i] = (byte) 0xff;
        }

        int len = buf_len;
        if (buf_len > 10) {
            len = 10;
        }
        try {
            System.arraycopy(buffer, 0, buff, 0, buf_len);
        } catch (Exception e) {
            for (int i = 0; (i < buf_len) && (i < len); i++) {
                buff[i] = buffer[i];
            }
            Log.w(TAG, "set auth start date error: " + e.toString());
        }
        byte[] temp = new byte[2];
        for (int i = 0; i < len; i++) {
            int total = 1;
            temp[0] = buff[i];
            SDKResult<Integer> integerSDKResult = SDK.hardwareAPI().eepromWrite(dm2016AuthStartDatePos + i, temp, total);
            length = integerSDKResult.getContent();
            if (length != total) {
                Log.e(TAG, "write setAuthStartDate failed");
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

    @Override
    public void setAuthStartDate(String date) {
        byte[] buffer = date.getBytes();
        setAuthStartDate(buffer, buffer.length);
    }

    // 获取授权失效日期
    @Override
    public String getAuthStartDate() {
        byte[] buff = new byte[32];
        int len = 0;
        int length = 0;

        len = 10;
        SDKHardwareResult mSDKHardwareResult = SDK.hardwareAPI().eepromRead(dm2016AuthStartDatePos, len);
        buff = mSDKHardwareResult.getByteArray();
        length = mSDKHardwareResult.getByteArray().length;
        if (length != len) {
            Log.e(TAG, "read getAuthStartDate failed");
        }

        String endDate = null;
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buff[i] == -1) {
                    break;
                }
            }
            endDate = new String(buff, 0, off + 1);
        } catch (Exception e) {
            Log.i(TAG, "get auth start date failed: " + e.toString());
            endDate = null;
        }
        Log.e(TAG, "startDate =" + endDate);
        return endDate;
    }

    // 设置累计播放时长
    @Override
    public void setWorkTime(byte[] buffer, int buf_len) {
        byte[] buff = new byte[32];
        int length = 0;

        for (int i = 0; i < 32; i++) {
            buff[i] = (byte) 0xff;
        }

        int len = buf_len;
        if (buf_len > 8) {
            len = 8;
        }
        try {
            System.arraycopy(buffer, 0, buff, 0, buf_len);
        } catch (Exception e) {
            for (int i = 0; (i < buf_len) && (i < len); i++) {
                buff[i] = buffer[i];
            }
            Log.w(TAG, "set work time error: " + e.toString());
        }
        byte[] temp = new byte[2];
        for (int i = 0; i < len; i++) {
            int total = 1;
            temp[0] = buff[i];
            SDKResult<Integer> integerSDKResult = SDK.hardwareAPI().eepromWrite(dm2016AuthDatePos + i, temp, total);
            length = integerSDKResult.getContent();
            if (length != total) {
                Log.e(TAG, "write setWorkTime failed");
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

    @Override
    public void setWorkTime(long value) {
        String strTime = value + "";
        int len = strTime.length();
        if (len != 8) {
            for (int i = 0; i < 8 - len; i++) {
                strTime = "0" + strTime;
            }
        }
        byte[] buffer = strTime.getBytes();
        setWorkTime(buffer, buffer.length);
    }

    //获取累计播放时长
    @Override
    public long getWorkTime() {
        byte[] buff = new byte[32];
        int len = 0;
        int length = 0;

        len = 8;
        SDKHardwareResult mSDKHardwareResult = SDK.hardwareAPI().eepromRead(dm2016AuthDatePos, len);
        buff = mSDKHardwareResult.getByteArray();
        length = mSDKHardwareResult.getByteArray().length;
        if (length != len) {
            Log.e(TAG, "read getWorkTime failed");
        }

        long date = 0;
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buff[i] == -1) {
                    break;
                }
            }
            date = Long.parseLong(new String(buff, 0, off + 1));
        } catch (Exception e) {
            Log.i(TAG, "get work time failed: " + e.toString());
            date = 0;
        }
        Log.e(TAG, "work time =" + date);
        return date;
    }
}
